package com.minglead.springboot.platform.sys.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.minglead.springboot.common.crud.BaseController;
import com.minglead.springboot.common.crud.BaseService;
import com.minglead.springboot.common.redis.JedisUtils;
import com.minglead.springboot.common.redis.JedisUtilsCluster;
import com.minglead.springboot.common.security.shiro.session.SessionDAO;
import com.minglead.springboot.common.utils.CookieUtils;
import com.minglead.springboot.common.utils.ObjectUtils;
import com.minglead.springboot.platform.sys.entity.User;
import com.minglead.springboot.platform.sys.security.UsernamePasswordToken;
import com.minglead.springboot.platform.sys.service.SystemService;
import com.minglead.springboot.platform.sys.utils.ActionResponse;
import com.minglead.springboot.platform.sys.utils.LogUtils;
import com.minglead.springboot.platform.sys.utils.UserUtils;

/**
 * 登录Controller
 */
@Controller
@RequestMapping(value = "${adminPath}")
public class LoginController extends BaseController{
	
	@Autowired
	private SessionDAO sessionDAO;
	
	@Autowired
	private SystemService systemService;
	
	@RequestMapping(value = "test")
	public String test(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return "test";
	}
	

	@RequestMapping(value = "toLogin")
	public String toLogin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		CookieUtils.removeCookie(request, response, "JSESSIONID");
		return "modules/sys/sysLogin";
	}
	
	
	/**
	 * 校验...
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "loginNew")
	public void loginNew(String username,String password,String validateCode,HttpServletRequest request, HttpServletResponse response){
		ActionResponse r = new ActionResponse(-1,"操作失败！");	
		//校验用户名密码
		try {
		       //解密
			   password=org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(password);
			   password = StringUtils.isEmpty(password)?password:(com.minglead.springboot.common.utils.StringUtils.uncompile(password));    
			   Boolean rememberMe= Boolean.getBoolean(String.valueOf(request.getAttribute("rememberMe")));		   
			   UsernamePasswordToken token = new UsernamePasswordToken(username, password,rememberMe,validateCode);  
			   Subject subject = UserUtils.getSubject();    
		        try {  
		            System.out.println("对用户[" + username + "]进行登录验证..验证开始");  
		            subject.login(token);             
		        }catch(IncorrectCredentialsException ae){        	
		            r.setMessage("密码错误!");
		    		this.responseObject(r, response);
		    		return;
		        }catch(AuthenticationException ae){        	
		            r.setMessage(ae.getMessage());
		    		this.responseObject(r, response);
		    		return;
		        }catch(Exception ae){  
		        	r.setMessage(ae.getMessage());
		    		this.responseObject(r, response);
		    		return;
		        }
		        //验证是否登录成功 （shiro自带密码校验是否正常） 
		        if(subject.isAuthenticated()){  
		            r.setCode(1);
		            r.setMessage("登录成功！");
		            LogUtils.saveLog(BaseService.getRequest(),UserUtils.getUser(),"系统登录");
		            //设置session	       ?	            
		    		this.responseObject(r, response);
		    		return;
					
		        }else{  
		            token.clear();  
		        }  
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.responseObject(r, response);
	}
	
	/**
	 * 登录成功，进入管理首页
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUserNoCache();
		request.setAttribute("user", user);
		CookieUtils.getCookie(request,"JSESSIONID");
		
		return "modules/sys/sysIndex";
	}
}